import { get, post, put, del } from '@/utils/request';
import type { User, LoginRequest, RegisterRequest, GetCodeRequest } from '@/types/api';
import type { OrderQueryParams, OrderListResponse } from '@/types/order';

/**
 * 用户相关API
 */
export class UserAPI {
  /**
   * 用户登录
   * @param loginData 登录数据
   * @returns Promise<any>
   */
  static async login(loginData: any): Promise<any> {
    const response = await post('/user/login', loginData);
    return response.data;
  }

  /**
   * 验证码
   * @param loginData 登录数据
   * @returns Promise<any>
   */
  static async getCode(loginData: GetCodeRequest): Promise<any> {
    const response = await post<any>('/user/verify_code', loginData);
    return response.data;
  }

  /**
   * 用户注册
   * @param registerData 注册数据
   * @returns Promise<User>
   */
  static async register(registerData: RegisterRequest): Promise<User> {
    const response = await post<User>('/auth/register', registerData);
    return response.data;
  }

  /**
   * 获取用户信息
   * @param userId 用户ID
   * @returns Promise<User>
   */
  static async getUserInfo(userId: number): Promise<User> {
    const response = await get<User>(`/users/${userId}`);
    return response.data;
  }

  /**
   * 更新用户信息
   * @param userId 用户ID
   * @param userData 用户数据
   * @returns Promise<User>
   */
  static async updateUser(userId: number, userData: Partial<User>): Promise<User> {
    const response = await put<User>(`/users/${userId}`, userData);
    return response.data;
  }

  /**
   * 删除用户
   * @param userId 用户ID
   * @returns Promise<void>
   */
  static async deleteUser(userId: number): Promise<void> {
    await del(`/users/${userId}`);
  }

  // 退出登录
  static async logout(): Promise<void> {
    await post(`/user/logout`);
  }

  /**
   * 获取用户详情
   * @param params 查询参数
   * @returns Promise<any<any>>
   */
  static async getUserProfile(registerData?: { uid?: number | string }) {
    return post<any>('/user/get_user_profile', registerData);
  }

  /**
   * 获取关注列表
   * @param params 查询参数
   * @returns Promise<any<any>>
   */
  static async getFollowings(params: { t_uid: number | string; start: number; size: number }) {
    return post<any>('/relation/get_followings', params);
  }

  /**
   * 获取粉丝列表
   * @param params 查询参数
   * @returns Promise<any<any>>
   */
  static async getFans(params: { t_uid: number | string; start: number; size: number }) {
    return post<any>('/relation/get_fans', params);
  }

  /**
   * 关注用户
   * @param params 关注参数
   * @returns Promise<any>
   */
  static async followUser(params: { to_id: number | string }) {
    return post<any>('/relation/follow', params);
  }

  /**
   * 取消关注用户
   * @param params 取消关注参数
   * @returns Promise<any>
   */
  static async unfollowUser(params: { to_id: number | string }) {
    return post<any>('/relation/unfollow', params);
  }

  /**
   * 获取互动消息
   * @returns Promise<any>
   */
  static async getNotifications(params: any) {
    return post<any>('/message/get_notifications', params);
  }

  /**
   * 获取系统消息
   * @returns Promise<any>
   */
  static async getSystemMessages(params: any) {
    return post<any>('/message/get_sys_msgs', params);
  }

  /**
   * 检查是否有新消息
   * @returns Promise<any>
   */
  static async hasMessage() {
    return post<any>('/message/has_msg');
  }

  /**
   * 设置消息为已读
   * @param params 消息参数
   * @returns Promise<any>
   */
  static async setMsgRead(params: any) {
    return post<any>('/message/set_msg_read', params);
  }

  // 修改用户信息
  static async updateUserProfile(info: any) {
    const response = await post<any>('/user/update_user_profile', info);
    return response.data;
  }

  // 获取用户通知设置
  static async getUserSetting() {
    const response = await post<any>('/user/user_setting');
    return response.data;
  }

  // 更新当前用户通知/偏好设置
  static async updateUserSetting(setting: any) {
    const response = await post<any>('/user/update_user_setting', setting);
    return response.data;
  }

  /**
   * 获取订单列表
   * @param params 查询参数
   * @returns Promise<any>
   */
  static async getOrders(params?: OrderQueryParams) {
    return post<any>('/pay/orders', params);
  }

  /**
   * Google 登录
   * @param loginData Google 登录数据（credential 或 access_token）
   * @returns Promise<any>
   */
  static async googleLogin(loginData: any): Promise<any> {
    const response = await post('/user/google_login', loginData);
    return response.data;
  }
}
